package com.tmp.alarm.task;

import com.tmp.alarm.utils.MySQLUtils;
import com.tmp.alarm.utils.PropertiesUtil;
import com.tmp.alarm.utils.UUIDByDateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.Connection;
import java.sql.ResultSet;

/**获取mart_mobile_memory_space表中最后(最新)一条数据进行做报警
 *
 * 小文件数据量占比告警
 *建表语句：
 CREATE TABLE small_file_perc_alarm (
 id int(11) NOT NULL AUTO_INCREMENT COMMENT '自增id',
 dt varchar(32) DEFAULT NULL COMMENT '文件日期',
 cluster_name varchar(32) COMMENT '集群名字',
 mart_name varchar(32) COMMENT '集市名字',
 small_file_num_perc int(11) DEFAULT NULL COMMENT '小文件占比',
 is_alarm int(11) DEFAULT NULL COMMENT '是否报警,1表示报警,2表示不报警',
 PRIMARY KEY (id)
 )
 */
public class SmallFileNumPercAlarm {

    private  final  static Logger LOGGER = LoggerFactory.getLogger(SmallFileNumPercAlarm.class);

    private final   static String small_file_num_perc = PropertiesUtil.get("small_file_num_perc").trim();


    /**
     *  100%>=小文件数量占比>=大于等于98%告警，按日更新
     *超过报警阀值的存入数据库
     */
    public static void smallFileNumPercAlarmAlarm(){

        int self_perc = Integer.parseInt(small_file_num_perc);
        String lev3dir_Small_File_Num_Perc_Alarm_sql = "select dt,cluster_name,mart_name,small_file_num_perc from mart_mobile_memory_space order by id desc";
        //更新已经查询过的记录,将type设置为2,供进度查询使用
        String update_sql = "UPDATE small_file_perc_alarm SET is_alarm=2 WHERE is_alarm=1";
        try {
            Connection connection =  MySQLUtils.getConnection();
            MySQLUtils.executeUpdate(connection, update_sql);
        }catch (Exception e){
            LOGGER.info("第一次运行，报警表为空");
        }
        try {
            Connection connection =  MySQLUtils.getConnection();
            ResultSet rs = MySQLUtils.query(connection, lev3dir_Small_File_Num_Perc_Alarm_sql);
            rs.next();
            int rows = rs.getRow();//得到当前行的行号
            rs.beforeFirst();
            if(rows >0){
                while (rs.next()){
                    String dt = rs.getString(1);
                    String cluster_name = rs.getString(2);
                    String mart_name = rs.getString(3);
                    String small_file_perc = rs.getString(4);
                    String twoPoint_perc = UUIDByDateUtils.twoPointNumbers(small_file_perc);
                    String twoPoint_perclen = twoPoint_perc.replaceAll("\\.", "");
                    int perc = Integer.parseInt(twoPoint_perclen);
                    String addsql = "insert into small_file_perc_alarm (dt,cluster_name,mart_name,small_file_num_perc,is_alarm) VALUES('"+ dt + "','"+ cluster_name +"','" + mart_name +"'," + perc +",1);";

                    if (self_perc<= perc){
                        try {
                            MySQLUtils.executeUpdate(connection, addsql);
                        }catch (Exception e){
                            LOGGER.info("超过阀值的数据插入数据库失败.....");
                        }
                    }else {
                        String add_sql = "insert into small_file_perc_alarm (dt,cluster_name,mart_name,small_file_num_perc,is_alarm) VALUES('"+ dt + "','"+ cluster_name +"','" + mart_name +"'," + perc +",2);";
                        MySQLUtils.executeUpdate(connection, add_sql);
                    }
                }

            }else{
                LOGGER.info("小文件占比告警没有查到数据,sql为:" + lev3dir_Small_File_Num_Perc_Alarm_sql);
            }
            MySQLUtils.returnConnection(connection);

        } catch (Exception e) {
            e.printStackTrace();
            LOGGER.info("小文件占比阀值告警异常...");
            throw new RuntimeException("小文件占比阀值告警异常..."+e.getMessage());
        }
    }


}
